{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4eb50795-d6a9-4339-a083-643f51a50911",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import AzureChatOpenAI\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "import openai\n",
    "from langchain.chains import GraphCypherQAChain\n",
    "from langchain.graphs import Neo4jGraph\n",
    "from langchain.callbacks import get_openai_callback\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "63905a82-6569-4e69-b757-04b187c33686",
   "metadata": {},
   "outputs": [],
   "source": [
    "LLM_MODEL = 'gpt-4'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "678addb6-d6b9-4447-893c-fb15099c7bf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/karthiksoman/anaconda3/envs/cypher_rag/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `AzureChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import AzureChatOpenAI`.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "\n",
    "load_dotenv(os.path.join(os.path.expanduser('~'), '.gpt_config.env'))\n",
    "API_KEY = os.environ.get('API_KEY')\n",
    "API_VERSION = os.environ.get('API_VERSION')\n",
    "RESOURCE_ENDPOINT = os.environ.get('RESOURCE_ENDPOINT')\n",
    "openai.api_type = \"azure\"\n",
    "openai.api_key = API_KEY\n",
    "openai.api_base = RESOURCE_ENDPOINT\n",
    "openai.api_version = API_VERSION\n",
    "chat_deployment_id = LLM_MODEL\n",
    "chat_model_id = chat_deployment_id\n",
    "temperature = 0\n",
    "chat_model = AzureChatOpenAI(openai_api_key=API_KEY, openai_api_version=API_VERSION, azure_deployment=chat_deployment_id, azure_endpoint=RESOURCE_ENDPOINT, temperature=temperature)\n",
    "load_dotenv(os.path.join(os.path.expanduser('~'), '.spoke_neo4j_config.env'))\n",
    "username = os.environ.get('NEO4J_USER')\n",
    "password = os.environ.get('NEO4J_PSW')\n",
    "url = os.environ.get('NEO4J_URL')\n",
    "database = os.environ.get('NEO4J_DB')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30a068c7-42d6-4f41-bce1-5fe2a38ace5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = Neo4jGraph(url=url, username=username, password=password, database = database)\n",
    "chain = GraphCypherQAChain.from_llm(chat_model, graph=graph, verbose=True, validate_cypher=True,return_intermediate_steps=True)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5b844002-f139-4dcf-9ddb-ebf42fcee2f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n",
      "Generated Cypher:\n",
      "\u001b[32;1m\u001b[1;3mMATCH (d:Disease {name: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name\u001b[0m\n",
      "Full Context:\n",
      "\u001b[32;1m\u001b[1;3m[{'g.name': 'COL2A1'}]\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "question = \"What are the genes associated with hypochondrogenesis?\"\n",
    "\n",
    "cypher_rag_answer = chain.invoke({\"query\":question})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "142b0c6d-b5b5-4c98-b5e3-c6f5f39bd30a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'query': 'What are the genes associated with hypochondrogenesis?',\n",
       " 'result': 'The gene associated with hypochondrogenesis is COL2A1.',\n",
       " 'intermediate_steps': [{'query': 'MATCH (d:Disease {name: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name'},\n",
       "  {'context': [{'g.name': 'COL2A1'}]}]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cypher_rag_answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4f99bfef-bcc2-462a-87b9-3c553978f8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'query': 'MATCH (d:Disease {name: \"hypochondrogenesis\"})-[:ASSOCIATES_DaG]->(g:Gene) RETURN g.name'},\n",
       " {'context': [{'g.name': 'COL2A1'}]}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cypher_rag_answer[\"intermediate_steps\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bd57161-2d8c-4c7e-99f5-09890b13fe27",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (cypher_rag)",
   "language": "python",
   "name": "cypher_rag"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
